package com.baitiaojun.video.api.impl;

import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.video.VideoChildComment;
import com.baitiaojun.api.domain.video.VideoComment;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.api.vo.PageInfo;
import com.baitiaojun.video.api.VideoCommentApi;
import com.baitiaojun.video.service.VideoChildCommentService;
import com.baitiaojun.video.service.VideoParentCommentService;
import com.baitiaojun.video.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @类名 VideoCommentApiImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/12/27 14:33
 * @版本 1.0
 */
@RestController
public class VideoCommentApiImpl implements VideoCommentApi {

    @Autowired
    private VideoParentCommentService videoParentCommentService;

    @Autowired
    private VideoChildCommentService videoChildCommentService;

    @Resource
    private UserSupport userSupport;

    @Autowired
    private HttpServletRequest request;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes addParentVideoComment(VideoComment videoComment) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        Long commentId = videoParentCommentService.addParentVideoComment(videoComment, userId);
        return JsonRes.builder().success().data(commentId);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes addChildVideoComment(VideoComment videoComment) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        videoChildCommentService.addChildVideoComment(videoComment, userId);
        return JsonRes.builder().success();
    }

    @Override
    public JsonRes getPageVideoCommentList(Integer pageNo, Long videoId) throws Exception {
        PageInfo pageInfo = videoParentCommentService.getPageVideoCommentList(pageNo, Integer.valueOf(ConstantUtil.VIDEO_COMMENT_PAGE_SIZE), videoId);
        return JsonRes.builder().success().data(pageInfo);
    }

    @Override
    public JsonRes getPageChildVideoCommentList(Integer pageNo, Integer pageSize, Long videoId, Long rootId) throws Exception {
        PageInfo pageInfo = videoChildCommentService.getPageChildVideoCommentList(pageNo, pageSize, videoId, rootId);
        return JsonRes.builder().success().data(pageInfo);
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes removeVideoChildComment(Long id) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        videoChildCommentService.removeVideoChildComment(id, userId);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes removeVideoComment(Long id) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        videoParentCommentService.removeVideoComment(id, userId);
        return JsonRes.builder().success();
    }

    @Override
    public JsonRes getReplayVideoCommentList() throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        List<VideoChildComment> videoChildCommentList = videoChildCommentService.getReplayVideoCommentList(userId);
        return JsonRes.builder().success().data(videoChildCommentList);
    }

    @Override
    public Integer getVideoCommentCount(Long videoId) {
        return videoParentCommentService.getVideoCommentCount(videoId);
    }

    @Override
    public JsonRes getAppendComments(Long commentId) {
        Object appendComments = videoParentCommentService.getAppendComments(commentId);
        return JsonRes.builder().success().data(appendComments);
    }
}
